const db_promise = require("../config/db.config.js");
const logger = require('../util/logger');

module.exports = {
  getConversations,
  createConversation,
  removeConversation
};

async function getConversations(req, res, next) {
  logger.winston.info('Conversations.getConversations');
  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from conversations where bot_id = ? order by timestamp desc",[req.params.bot_id]);
    	 res.status(200).json(rows);
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }   
    

}

async function createConversation(req, res, next) {
  logger.winston.info('Conversations.createConversation');
  
    try {
    	
			  const [rows,fields] = await db_promise.query("insert into conversations(bot_id) values (?)", [req.body.bot_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }    
  

}

async function removeConversation(req, res, next) {
  logger.winston.info('Conversations.removeConversation');

  
    try {
    	
			  const [rows,fields] = await db_promise.query("delete from conversations where conversation_id = ?", [req.query.conversation_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }
	    
}